<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Artists\Message;
use App\Models\Artists\ArtistsUserClass;
use App\Models\Artists\JoinArtist;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\Category;
use App\Admin\Extensions\Tools\GridView;

class MessageController extends Controller
{
    protected $name = '留言管理';
    use ModelForm;
	
	public $user_id = 0;
	
	public function __construct()
	{
		//查询艺术家的user_id
		$info = JoinArtist::where('artists_id', request()->get('artists_id'))->first();
		if($info)
		{
			$this->name = $info->name.'的'.$this->name;
			$this->user_id = $info->user_id;
		}
		
	}
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Message::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            
            $grid->filter(function($filter){
                
                $filter->like('content', '标题');
                //$filter->addFilter($filter->like('post_title', '标题'));
                // $filter->equal('item_id', '艺术家ID');
                
            
            });
            $grid->column('id', 'ID')->sortable();
            // $grid->column('listorder', '排序')->editable();
            
            $grid->column('content', '内容')->limit(50);
            $grid->column('type', '类型');
            $grid->column('remark', '回复');
            $grid->column('status','状态')->options()->select(Message::$status);

            $grid->column('created_at', '留言时间');
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        
        return Admin::form(Message::class, function (Form $form) {
            
            $form->number('id', 'ID')->attribute('disabled', false);
            $form->textarea('content', '内容')->rules('required|min:1');
            $form->textarea('remark', '回复')->rules('required|min:1');
            $form->radio('status','状态')->options(Message::$status);

			
			$form->saving(function(Form $form){
				
			});
			
            
        });
    }
}
